<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>计算属性的复杂操作</title>
  <script src="../js/vue.min.js"></script>
</head>

<body>
  <div id="app">
    <h2>总价格： {{ totalPrice }}</h2>
  </div>

  <script>
    var vm = new Vue({
      el: '#app',
      data: {
        books: [
          { id: 110, name: "《java入门到精通》",  price: 119},
          { id: 111, name: "计算机网络",  price: 59},
          { id: 112, name: "《代码大全》",  price: 119},
          { id: 113, name: "《操作系统》",  price: 109},
          { id: 114, name: "《java入门到精通》",  price: 119.01}
        ]
      },
      computed: {
        // 多次调用只会执行一次
        // 计算属性是有缓存的
        totalPrice() {
          let result = 0;
          // 初级
          // for( let i=0; i< this.books.length; i++) {
          //   result += this.books[i].price;
          // }
          //ES6
          // for(let i in this.books) {
          //   result += this.books[i].price;
          // }
          for (let book of this.books) {
            result += book.price;
          }
          return result;
        }
      },
      methods: {

      }
    });
  </script>
</body>

</html>